package com.cuitrip.business.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;

import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.OnClick;

public class ShowCertInfoFragment extends CustomUiFragment {
    public static final String SHOW_CERT_KEY = "SHOW_CERT_KEY";
    public static final int SHOW_CERT_EMAIL = 1;
    public static final int SHOW_CERT_PHONE = 2;
    public static final int REQUEST_CODE_CERT = 3;
    public static final String RESULT_DATA = "RESULT_DATA";
    private boolean canChange;

    enum CertType {
        email {
            @Override
            public void init(Context context) {
                title = context.getResources().getString(R.string.mail_verify);
                button = context.getResources().getString(R.string.mail_change);
                drawable=R.drawable.mail_ok;
            }

            @Override
            public String getValue(FragmentActivity activity) {
                try {
                    String value = LoginInstance.getInstance().getUserInfo().getEmail();
                    return value;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            public Intent getCertIntent(Fragment fragment) {
                TrackUtil.trackScreenName("/user/verification/email/set/address");
                Intent intent = new Intent(fragment.getActivity(), CertEmailActivity.class);
                String value = getValue(fragment.getActivity());
                intent.putExtra(CertEmailActivity.KEY_VALUE, value);
                return intent;
            }
        }, phone {
            @Override
            public void init(Context context) {
                title = context.getResources().getString(R.string.phone_verify);
                button = context.getResources().getString(R.string.phone_change);
                drawable=R.drawable.phone_ok;
            }

            @Override
            public String getValue(FragmentActivity activity) {
                try {
                    CtUserInfo ctUserInfo = LoginInstance.getInstance().getUserInfo();
                    if (!TextUtils.isEmpty(ctUserInfo.getMobile())) {
                        StringBuilder builder = new StringBuilder();
                        if (!TextUtils.isEmpty(ctUserInfo.getCountryCode())) {
                            builder.append("+").append(ctUserInfo.getCountryCode()).append(" ");
                        }
                        builder.append(ctUserInfo.getMobile());
                        return builder.toString();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            public Intent getCertIntent(Fragment fragment) {
                TrackUtil.trackScreenName("/user/verification/phone/set/number");
                Intent intent = new Intent(fragment.getActivity(), CertPhoneActivity.class);
                return intent;
            }
        };
        public String button;
        public String title;
        public int drawable;

        public abstract void init(Context context);

        public static CertType getType(int type) {
            switch (type) {
                case SHOW_CERT_EMAIL:
                    return email;
                case SHOW_CERT_PHONE:
                    return phone;
            }
            return null;
        }

        public abstract String getValue(FragmentActivity activity);

        public abstract Intent getCertIntent(Fragment fragment);
    }

    CertType certType;
    @Bind(R.id.value)
    TextView valueView;
    @Bind(R.id.image)
    ImageView image;
    @Bind(R.id.button)
    Button button;

    @Override
    public void onTopbarLeftPress() {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            activity.finish();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_show_cert_info);
        ButterKnife.bind(this, view);
        //判断资料是否为空
        String value = certType.getValue(getActivity());
        CtUserInfo ctUserInfo = LoginInstance.getInstance().getUserInfo();
        if ((certType == CertType.phone && TextUtils.isEmpty(value)) ||
                (certType == CertType.email && ctUserInfo.isEmailValidated() == false)) {
            Intent intent = certType.getCertIntent(this);
            startActivityForResult(intent, REQUEST_CODE_CERT);
        } else {//资料不为空，填充页面
            valueView.setText(value);
        }
            button.setText(certType.button);
            button.setTextColor(ResourceUtil.getThemeColor());
            image.setImageResource(certType.drawable);
        return view;
    }

    @OnClick(R.id.button)
    void clickButton() {
        if (canChange) {
            Intent intent = certType.getCertIntent(this);
            startActivityForResult(intent, REQUEST_CODE_CERT);
        } else {
            getActivity().finish();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != REQUEST_CODE_CERT) {
            return;
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            getActivity().finish();
        } else {
            //// TODO: 2015/12/22  更改显示和事件
            String value = data.getStringExtra(RESULT_DATA);
            valueView.setText(value);
            canChange = false;
            button.setText(R.string.operation_ok_2);
        }
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        //获取显示类型，邮箱还是电话
        Bundle arguments = getArguments();
        int type = arguments.getInt(SHOW_CERT_KEY);
        certType = CertType.getType(type);
        certType.init(getActivity());
        canChange = true;
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = certType.title;
        return config;
    }

}